
package com.example.moshudriver.model;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.BeeFramework.model.BaseModel;
import com.BeeFramework.model.BeeCallback;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.locationinfoRequest;
import com.example.moshudriver.protocol.locationinfoResponse;
import com.example.moshudriver.tools.LocationManager;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.external.androidquery.callback.AjaxStatus;

public class LocationInfoModel extends BaseModel {

	public String publicLocationName = "";
	public LocationInfoModel(Context context) {
		super(context);
	}
	
	public void get() {
		locationinfoRequest request = new locationinfoRequest();
		request.ver = O2OMobileAppConst.VERSION_CODE;
		request.lat = LocationManager.getLatitude();
		request.lon = LocationManager.getLongitude();
		
		BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

			@Override
			public void callback(String url, JSONObject jo, AjaxStatus status) {
				try {
					LocationInfoModel.this.callback(this, url, jo, status);
                    if (null != jo) {
                        locationinfoResponse response = new locationinfoResponse();
                        response.fromJson(jo);
                        if(response.succeed == 1) {
                        	if(response.location != null && response.location.name != null) {
                                publicLocationName = response.location.name;
                        	}
                        	LocationInfoModel.this.OnMessageResponse(url,jo,status);
                        } else {
                        	LocationInfoModel.this.callback(url, response.error_code, response.error_desc);
                        }
                    }
				} catch (JSONException e) {

				}
			}
		};

		Map<String, Object> params = new HashMap<String, Object>();
		try {
			params.put("json", request.toJson().toString());
		} catch (JSONException e) { 
			
		}
		cb.url(ApiInterface.LOCATION_INFO).type(JSONObject.class).params(params);
        ajax(cb);
	}
	
}
